import moment from "moment";
import { Modal } from "antd";
const _ = require('lodash');

// 活动状态
export const menus = [
  { id: 0, name: "全部" },
  { id: 2, name: "进行中" },
  { id: 3, name: "未开始" },
  { id: 5, name: "已结束" },
  { id: 1, name: "待审核" },
  { id: 4, name: "已驳回" },
  { id: 6, name: "已关闭" },
  { id: 7, name: "草稿箱" },
];

export const activityType = [
  { id: 0, name: "基本信息" },
  { id: 1, name: "活动信息" }
]

export const acttype = [
  { id: 1, name: "报名", key: 'isSignUp', status: false },
  { id: 2, name: "投票", key: 'isVote', status: false },
  { id: 3, name: "门票", key: 'isRobTickets', status: false },
  { id: 4, name: "签到", key: 'isSignIn', status: false },
  { id: 5, name: "抽奖", key: 'isLuckyDraw', status: false },
];

export const namearr = [
    { key: "姓名", value: "", inputType: 1, length: 30, inputVerifyType: "BaseInput7", required: true, isActive: true },
    { key: "性别", value: "", inputType: 4, length: "", inputVerifyType: "", required: true, isActive: false },
    { key: "图片", value: "", inputType: 5, length: "", inputVerifyType: "", required: true, isActive: false, pictureUrl: "" },
    { key: "手机号", value: "", inputType: 2, length: 11, inputVerifyType: "MobileNoInput", required: true, isActive: false },
    { key: "生日", value: "", inputType: 3, length: "", inputVerifyType: "", required: true, isActive: false },
    { key: "QQ号", value: "", inputType: 2, length: 20, inputVerifyType: "PwdInput5", required: true, isActive: false },
    { key: "邮箱", value: "", inputType: 1, length: 30, inputVerifyType: "BaseInput7", required: true, isActive: false },
    { key: "学院", value: "", inputType: 1, length: 30, inputVerifyType: "BaseInput7", required: true, isActive: false },
    { key: "年级", value: "", inputType: 1, length: 30, inputVerifyType: "BaseInput7", required: true, isActive: false },
    { key: "班级", value: "", inputType: 1, length: 10, inputVerifyType: "BaseInput1", required: true, isActive: false },
    { key: "学号", value: "", inputType: 1, length: 10, inputVerifyType: "BaseInput1", required: true, isActive: false },
    { key: "特长", value: "", inputType: 1, length: 30, inputVerifyType: "BaseInput7", required: true, isActive: false },
    { key: "备注", value: "", inputType: 1, length: 30, inputVerifyType: "BaseInput7", required: true, isActive: false },
]

export const signupFormlistprojrctName = {
    value: "",
    inputType: 1,
    length: 30,
    inputVerifyType: "BaseInput7",
    required: true,
}

// 数组 - 深拷贝
// export const cloneArray = (val) => val.concat();
export const cloneArray = (val) => JSON.parse(JSON.stringify(val));

// 时间格式显示
export const dateFormat = "YYYY-MM-DD";
export const dateFormatTime = "YYYY-MM-DD HH:mm";
export const setdates = (v) => moment(v).format("YYYY-MM-DD");
export const setdateDates = (v) => moment(new Date(v)).format("YYYY-MM-DD");
export const setdateTimes = (v) => moment(new Date(v)).format("YYYY-MM-DD HH:mm");
export const disabledDate = (current) => current && current < moment().startOf("day");
export const disabledDateEnd = (current) => current && current > moment().endOf("day");

// 数组 - 删除数组元素
export const deleteArrayTerm = (arr = [], val) => {
  if (arr.includes(val)) {
    arr = arr.filter((v) => v !== val);
  } else {
    arr.push(val);
  }
  return [...arr];
};

// 对象 - 置空
export const resetdates = (params, obj = {}) => {
  // for (let v in params) obj[v] = null;
  return obj;
};

// 对象 - 判断空对象
export const isnullObj = (a) => {
    let len = Object.keys(a);
    return (len.length === 0);
}

// 对象 - 合并对象
export const mergeObj = (a, b) => {
    return {...a, ...b}
};

// 对象 - 通过id获取对象name
export const filMenuState = (i) => {
    let result = menus.find((obj) => obj.id === i);
    return result.name || "未知";
}

// 对象 - 过滤
export const filMenuValue = (i, arr = [], obj = {}) => {
  arr = i === 5 ? [i, i + 1] : i === 7 ? true : [i];
  i === 0
    ? (obj = {})
    : i === 7
    ? (obj["isDraft"] = arr)
    : (obj["activityStatus"] = arr);
  return obj;
};

// 对象 - 对象value值为真
export const filMenuFormWhite = (params, obj = {}) => {
    for (let v in params) if (params[v]) obj[v] = params[v];
    return obj;
};

// 对象深拷贝
export const deepCopy = (objOrArr) => {
  // let objClone = Array.isArray(objOrArr) ? [] : {};
  // if (objOrArr && typeof objOrArr === "object") {
  //   for (let key in objOrArr) {
  //     if (objOrArr.hasOwnProperty(key) && objOrArr[key] !== undefined) {
  //       //判断ojb子元素是否为对象，如果是，递归复制
  //       if (objOrArr[key] && typeof objOrArr[key] === "object") {
  //         objClone[key] = deepCopy(objOrArr[key]);
  //       } else {
  //         //如果不是，简单复制
  //         objClone[key] = objOrArr[key];
  //       }
  //     }
  //   }
  // }
  let objClone = _.cloneDeep(objOrArr)
  return objClone;
};

// 开始结束时间过滤
export const filMenuForm = (params, obj = {}) => {
  for (let v in params) if (params[v]) obj[v] = params[v];
  let { rangepicker } = params;
  if (rangepicker) {
    obj["queryStartDate"] = setdates(rangepicker[0]);
    obj["queryEndDate"] = setdates(rangepicker[1]);
    delete obj["rangepicker"];
  }
  return obj;
};

// 用户状态
export const filIsExpose = (v) => (v ? "所有用户" : "白名单用户");

// 图片上传格式
export const checkImageWH = (file) => {
  return new Promise(function (resolve, reject) {
    let filereader = new FileReader();
    filereader.onload = (e) => {
      let src = e.target.result;
      const image = new Image();
      image.onload = function () {
        // 获取图片的宽高，并存放到file对象中
        file.width = this.width;
        file.height = this.height;
        resolve();
      };
      image.onerror = reject;
      image.src = src;
    };
    filereader.readAsDataURL(file);
  });
};
export const handleBeforeUpload = (file) => {
  //限制图片 格式、size、分辨率
  const isJPG = file.type === "image/jpeg";
  const isGIF = file.type === "image/gif";
  const isPNG = file.type === "image/png";
  if (!(isJPG || isGIF || isPNG)) {
    Modal.error({
      title: "只能上传JPG 、JPEG 、GIF、 PNG格式的图片~",
    });
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    Modal.error({
      title: "超过2M限制 不允许上传~",
    });
  }
  return (isJPG || isGIF || isPNG) && isLt2M && checkImageWH(file);
};

export const layout = {
  labelCol: {
    span: 18,
  },
  wrapperCol: {
    span: 18,
  },
};

export const menuNav = {
  menus,
};
